import {
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast
} from "../../utils/asyncWx.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cart: [],
    address: {},
    totalprice: 0,
    totalnum: 0,
    allchecked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow() {

    const cart = wx.getStorageSync("cart")
    const address = wx.getStorageSync("address")
    this.setData({
      cart,
      address
    })
    this.setMoney(cart)
  },
  async handleChooseAddress() {

    try {
      const res1 = await getSetting()
      const scopeAddress = res1.authSetting['scope.address']
      console.log(scopeAddress)
      if (scopeAddress === false) {
        await openSetting()
      }
      let address = await chooseAddress();
      address.all = address.provinceName + address.cityName + address.countyName + address.detailInfo;

      // 5 存入到缓存中
      wx.setStorageSync("address", address);
    } catch (error) {
      console.log(err)
    }
  },
  //计算总价格
  setMoney(cart) {
    let allchecked = true
    let totalprice = 0;
    let totalnum = 0;
    cart.forEach(v => {
      if (v.checked) {

        totalprice = totalprice + v.num * v.goods_price;
        totalnum = totalnum + v.num
      } else {
        allchecked = false
      }
    })
    this.setData({
      cart,
      totalnum,
      totalprice,
      allchecked
    })
    wx.setStorageSync("cart", cart)
  },
  //跳转到支付授权页面
  handlePay() {
    wx.navigateTo({
      url: '/pages/auth/index',
    })
  }

})